﻿using InterfaceComponents;
using Microsoft.Xna.Framework;
using DrawableObjects.Actors.Projectiles;
using System.Collections.Generic;
using System;
using SpecialEffects;

namespace DrawableObjects.Actors.Weapons 
{
    public class Weaponsystem
    {
	    public HUDWrapper<int> level
	    {
		    get;
		    private set;
	    }

	    private Weapon[] weapons
	    {
		    get;
		    set;
	    }

        private Vector2[] launchPoints;

        public float[] launchAngles = new float[1]{0};


        private int slotCounter = 0;

	    public Weapon.WeaponType weaponCategory
	    {
		    get;
		    private set;
	    }

        public void Update()
        {
            foreach (Weapon weapon in weapons)
            {
                weapon.Update();
            }
        }

        /// <summary>
        /// Upgrades the weaponsystem if it is not already full upgraded
        /// </summary>
	    public bool Upgrade()
	    {
            if (this.level.value < weapons.Length - 1)
            {
                this.level.value += 1;
                return true;
            }
            return false;
	    }

        /// <summary>
        /// these are factory methods to create the different weaponsystems for the playership, enemy and the drone
        /// </summary>
        public static Weaponsystem GET_WEAPON_PLAYER_PLASMA(Vector2[] launchPoints)
	    {
            Weapon lvl1 = new Weapon(Plasma.GET_PLASMA_LEVEl1(), 700);
            Weapon lvl2 = new Weapon(Plasma.GET_PLASMA_LEVEl2(), 700);
            Weapon lvl3 = new Weapon(Plasma.GET_PLASMA_LEVEl3(), 500);
            Weapon[] weapons = new Weapon[3] {lvl1, lvl2, lvl3};
            return new Weaponsystem(launchPoints, Weapon.WeaponType.Plasma, weapons);

	    }

        public static Weaponsystem GET_WEAPON_PLAYER_LASER(Vector2[] launchPoints)
	    {
            Weapon lvl1 = new Weapon(Laser.GET_LASER_LEVEl1(), 500f);
            Weapon lvl2 = new Weapon(Laser.GET_LASER_LEVEl2(), 500f);
            Weapon lvl3 = new Weapon(Laser.GET_LASER_LEVEl3(), 500f);
            Weapon[] weapons = new Weapon[3] { lvl1, lvl2, lvl3 };
            return new Weaponsystem(launchPoints, Weapon.WeaponType.Laser, weapons);
	    }

        public static Weaponsystem GET_WEAPON_PLAYER_MISSILE(Vector2[] launchPoints)
	    {
            Weapon lvl1 = new Weapon(Missile.GET_MISSILE_LEVEL1(), 300);
            Weapon lvl2 = new Weapon(Missile.GET_MISSILE_LEVEL2(), 300);
            Weapon lvl3 = new Weapon(Missile.GET_MISSILE_LEVEL3(), 300);
            Weapon[] weapons = new Weapon[3] { lvl1, lvl2, lvl3 };
            return new Weaponsystem(launchPoints , Weapon.WeaponType.Missile, weapons);
	    }


        public static Weaponsystem GET_WEAPON_BOSS_LIGHTNING(Vector2 launchPoint)
	    {
            Weapon BOSSMODUS = new Weapon(Lightning.GET_LIGHTNING_BOSS(), 1000);
            Weapon[] weapons = new Weapon[1] {BOSSMODUS};
            return new Weaponsystem(new Vector2[] { launchPoint }, Weapon.WeaponType.Lightning, weapons);
	    }

        public static Weaponsystem GET_WEAPON_BOSS_MISSILE(Vector2[] launchPoints)
	    {
            Weapon[] weapons = new Weapon[1] { 
                new Weapon(Missile.GET_MISSILE_ENEMY(), 400)
            };
            return new Weaponsystem(launchPoints, Weapon.WeaponType.Missile, weapons);
	    }

        public static Weaponsystem GET_WEAPON_BOSS_LASER(Vector2[] launchPoints)
	    {
            Weapon BOSSMODUS = new Weapon(Laser.GET_LASER_BOSS(), 100f);
            Weapon[] weapons = new Weapon[1] { BOSSMODUS };
            return new Weaponsystem(launchPoints, Weapon.WeaponType.Laser, weapons);
	    }

        public static Weaponsystem GET_WEAPON_BOSS_SONARWAVE(Vector2 launchPoint)
	    {
            Weapon BOSSMODUS = new Weapon(SonarWave.GET_SONARWAVE_BOSS(), 50000f);
            Weapon[] weapons = new Weapon[1] { BOSSMODUS };
            return new Weaponsystem(new Vector2[] { launchPoint }, Weapon.WeaponType.Sonarwave, weapons);
	    }

        public static Weaponsystem GET_WEAPON_HEALER(Vector2 launchPoint)
        {
            Weapon HEAL = new Weapon(HealingBeam.GET_HEALING_BEAM_DRONE(), 1500f);
            Weapon[] weapons = new Weapon[1] { HEAL };
            return new Weaponsystem(new Vector2[] { launchPoint }, Weapon.WeaponType.Heal, weapons);
        }

        public static Weaponsystem GET_WEAPON_ENEMY(int baseDamage, Vector2 launchPoint, Color color)
        {
            Weapon lvl1 = new Weapon(Plasma.GET_PLASMA_ENEMY(baseDamage, color), 4500);
            Weapon[] weapons = new Weapon[1] { lvl1 };
            return new Weaponsystem(new Vector2[] { launchPoint }, Weapon.WeaponType.Plasma, weapons);
        }

	    public bool IsReady()
	    {
            bool isReady = false;
            if (weapons != null)
            {
                if (weapons.Length > level.value)
                {
                    isReady = weapons[level.value].isReady();
                }
            }
            return isReady;
	    }

	    public Projectile Fire(Vector2 direction, Vector2 shipPos)
	    {
            Projectile projectile = null;
            if (weapons != null)
            {
                if (weapons.Length > level.value)
                {
                    Vector2 transformed = Vector2.Transform(direction, Matrix.CreateRotationZ(launchAngles[slotCounter]));
                    projectile = weapons[level.value].fire(transformed, Vector2.Add(launchPoints[slotCounter], shipPos));
                    slotCounter ++;
                    slotCounter %= launchPoints.Length;

                }
            }
            return projectile;
	    }


	    public Weaponsystem(Vector2[] launchPoints, Weapon.WeaponType weaponCategory, Weapon[] weapons)
	    {
            this.level = new HUDWrapper<int>(new List<HUDComponent>());
            this.level.value = 0;
            this.launchPoints = launchPoints;
            this.weaponCategory = weaponCategory;
            this.weapons = weapons;
            launchAngles = new float[launchPoints.Length];
            for (int i = 0; i < launchPoints.Length; i++) {
                launchAngles[i] = 0;
            }

	    }

    }
}

